package com.yaodian100.mall.tuangou.web;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.common.service.BaseManager;
import com.yaodian100.ec.product.entity.Product;
import com.yaodian100.ec.product.service.Product2Service;
import com.yaodian100.ec.shelf.entity.ShelfTuangou;
import com.yaodian100.ec.shelf.service.ShelfTuangouService;

public class TuangouAction extends ActionSupport implements Preparable {

	private static final long serialVersionUID = 6000921563831671257L;
	private final Logger logger = LoggerFactory.getLogger(TuangouAction.class);
	@Resource
	private ShelfTuangouService shelfTuangouService;
	@Resource
	private BaseManager baseMgr;
	@Resource
	private Product2Service product2Service;

	private String tuangouId;
	private ShelfTuangou shelfTuangou;
	private Product product;
	private BigDecimal discount;
	private long surplusTime;
	private List<ShelfTuangou> todayTuangouList;
	private Map<String, String> todayPrdNameMap;
	private int inventoryCount;
	private List<ShelfTuangou> historyTuangouList;
	private Map<String, Product> getHistoryPrdMap;
	private Map<String, BigDecimal> discountMap;
	private int historyTuangouListSize;
	private SimplePager pager;
	private Map<String, Integer> invMap;
	private String pageTitle;
	private String pageDescription;
	private String pageKeywords;

	@Override
	public void prepare() throws Exception {
		if (null == pager) {
			pager = new SimplePager(1);
		}
	}

	public String activity() throws Exception {
		pageTitle = "耀团购 - 享受诚信满意的购物体验 - 耀点100 ";
		pageDescription = "耀团购,耀点100精选团购商品,提供女装、男装、童装,内睡衣,鞋子,配饰,家居生活等各类商品,100%正品,支持免费退换货,让您享受诚信满意的购物体验。";
		pageKeywords = "团购,tuangou,耀团购,耀点100,YD100,yaodain100";
		return "activity";
	}

	public String history() throws Exception {
		pageTitle = "精采回顾 - 耀团购 - 享受诚信满意的购物体验 - 耀点100 ";
		pageDescription = "耀团购,耀点100精选团购商品,提供女装、男装、童装,内睡衣,鞋子,配饰,家居生活等各类商品,100%正品,支持免费退换货,让您享受诚信满意的购物体验。";
		pageKeywords = "精采回顾,过往团购,往期团购,团购,tuangou,耀团购,耀点100,YD100,yaodain100";
		historyTuangouList = shelfTuangouService.getHistoryTuangouList(0, -1);
		historyTuangouListSize = historyTuangouList.size();
		if (historyTuangouList.size() > 0) {
			pager.setPageRecord(20);// defaut 20
			pager.setTotalSize(historyTuangouListSize);

			historyTuangouList = shelfTuangouService.getHistoryTuangouList(pager.getCurrentPage() * pager.getPageRecord(),
					pager.getPageRecord());

			getHistoryPrdMap = shelfTuangouService.getPrdMap(historyTuangouList);
			discountMap = new TreeMap<String, BigDecimal>();
			for (int i = 0; i < historyTuangouList.size(); i++) {
				ShelfTuangou historyTuangou = historyTuangouList.get(i);
				// logger.error("historyTuangou.getTuangouId():{}", historyTuangou.getTuangouId());
				discount = historyTuangou.getPrice()
						.divide(getHistoryPrdMap.get(historyTuangou.getProductId()).getListPrice(), 2, BigDecimal.ROUND_HALF_UP)
						.multiply(new BigDecimal(10));
				discountMap.put(historyTuangou.getProductId(), discount);
			}
			invMap = shelfTuangouService.getInvetoryMap(getHistoryPrdMap);

		}
		return "history";
	}

	public String product() throws Exception {
		Date now = new Date();
		shelfTuangou = shelfTuangouService.getShelfTuangouByTuangouId(new Long(tuangouId), now, null);
		if (shelfTuangou == null)
			return "outSale";
		product = (Product) baseMgr.get(Product.class, shelfTuangou.getProductId());
		if (product == null)
			return "outSale";
		int chkPrdType = product.getProductType();
		String chkIsDel = product.getIsDelete();
		if (!((chkPrdType == 0 || chkPrdType == 1 || chkPrdType == 7 || chkPrdType == 2) && "N".equals(chkIsDel)))
			return "outSale";
		inventoryCount = product2Service.getSpecificationInventorySum(shelfTuangou.getProductId());

		discount = shelfTuangou.getPrice().divide(product.getListPrice(), 2, BigDecimal.ROUND_HALF_UP)
				.multiply(new BigDecimal(10));

		surplusTime = shelfTuangou.getEndDate().getTime() - now.getTime();

		todayTuangouList = shelfTuangouService.getTodayTuangouList(new Long(tuangouId));
		todayPrdNameMap = shelfTuangouService.getTodayPrdNameMap(todayTuangouList);

		pageTitle = shelfTuangou.getPromoteName() + " - " + product.getProductName() + " - 耀团购 ";
		pageDescription = shelfTuangou.getPromoteName() + "," + product.getProductName();
		pageKeywords = product.getProductName() + "团购," + product.getProductName() + "," + shelfTuangou.getPromoteName()
				+ ",团购,耀团购,耀点100";
		return "product";
	}

	// getter & setter
	public ShelfTuangouService getShelfTuangouService() {
		return shelfTuangouService;
	}

	public void setShelfTuangouService(ShelfTuangouService shelfTuangouService) {
		this.shelfTuangouService = shelfTuangouService;
	}

	public Product2Service getProduct2Service() {
		return product2Service;
	}

	public void setProduct2Service(Product2Service product2Service) {
		this.product2Service = product2Service;
	}

	public String getTuangouId() {
		return tuangouId;
	}

	public void setTuangouId(String tuangouId) {
		this.tuangouId = tuangouId;
	}

	public ShelfTuangou getShelfTuangou() {
		return shelfTuangou;
	}

	public void setShelfTuangou(ShelfTuangou shelfTuangou) {
		this.shelfTuangou = shelfTuangou;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public BigDecimal getDiscount() {
		return discount;
	}

	public void setDiscount(BigDecimal discount) {
		this.discount = discount;
	}

	public long getSurplusTime() {
		return surplusTime;
	}

	public void setSurplusTime(long surplusTime) {
		this.surplusTime = surplusTime;
	}

	public List<ShelfTuangou> getTodayTuangouList() {
		return todayTuangouList;
	}

	public void setTodayTuangouList(List<ShelfTuangou> todayTuangouList) {
		this.todayTuangouList = todayTuangouList;
	}

	public Map<String, String> getTodayPrdNameMap() {
		return todayPrdNameMap;
	}

	public void setTodayPrdNameMap(Map<String, String> todayPrdNameMap) {
		this.todayPrdNameMap = todayPrdNameMap;
	}

	public int getInventoryCount() {
		return inventoryCount;
	}

	public void setInventoryCount(int inventoryCount) {
		this.inventoryCount = inventoryCount;
	}

	public List<ShelfTuangou> getHistoryTuangouList() {
		return historyTuangouList;
	}

	public void setHistoryTuangouList(List<ShelfTuangou> historyTuangouList) {
		this.historyTuangouList = historyTuangouList;
	}

	public Map<String, Product> getGetHistoryPrdMap() {
		return getHistoryPrdMap;
	}

	public void setGetHistoryPrdMap(Map<String, Product> getHistoryPrdMap) {
		this.getHistoryPrdMap = getHistoryPrdMap;
	}

	public Map<String, BigDecimal> getDiscountMap() {
		return discountMap;
	}

	public void setDiscountMap(Map<String, BigDecimal> discountMap) {
		this.discountMap = discountMap;
	}

	public int getHistoryTuangouListSize() {
		return historyTuangouListSize;
	}

	public void setHistoryTuangouListSize(int historyTuangouListSize) {
		this.historyTuangouListSize = historyTuangouListSize;
	}

	public SimplePager getPager() {
		return pager;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public Map<String, Integer> getInvMap() {
		return invMap;
	}

	public void setInvMap(Map<String, Integer> invMap) {
		this.invMap = invMap;
	}

	public String getPageTitle() {
		return pageTitle;
	}

	public void setPageTitle(String pageTitle) {
		this.pageTitle = pageTitle;
	}

	public String getPageDescription() {
		return pageDescription;
	}

	public void setPageDescription(String pageDescription) {
		this.pageDescription = pageDescription;
	}

	public String getPageKeywords() {
		return pageKeywords;
	}

	public void setPageKeywords(String pageKeywords) {
		this.pageKeywords = pageKeywords;
	}
}